package com.selune.online.ordering.system.project.tenant.controller;

import com.selune.online.ordering.system.project.api.base.response.Response;
import com.selune.online.ordering.system.project.api.pojo.user.dto.LoginDTO;
import com.selune.online.ordering.system.project.api.pojo.user.tenant.dto.TenantLoginDTO;
import com.selune.online.ordering.system.project.api.pojo.user.tenant.dto.TenantUserInfoDTO;
import com.selune.online.ordering.system.project.api.pojo.user.tenant.vo.TenantUserInfoVO;
import com.selune.online.ordering.system.project.api.service.tenant.ITenantUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 商户用户控制器
 *
 * @author xiaoyp
 * @date 2020/2/27
 */

@RestController
@RequestMapping("/api/v1/ordering/tenant/user")
@Slf4j
@Api(value = "商户用户接口")
public class TenantUserController {
	
	@Autowired
	private ITenantUserService iTenantUserService;
	
	@ApiOperation(value = "注册租户用户", notes = "注册租户用户")
	@ApiImplicitParam(name = "tenantUserInfoDTO", value = "用户信息DTO", required = true, dataType = "tenantUserInfoDTO", paramType = "body")
	@PostMapping(value = "/register", produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Response<Boolean> saveTenantUser(@RequestBody TenantUserInfoDTO tenantUserInfoDTO) {
		return iTenantUserService.saveTenantUser(tenantUserInfoDTO);
	}
	
	@ApiOperation(value = "根据用户id查询用户", notes = "根据用户id查询用户")
	@ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "Long", paramType = "query")
	@GetMapping(value = "/info/{userId}", produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Response<TenantUserInfoVO> getTenantUser(@PathVariable("userId") Long userId) {
		return iTenantUserService.getTenantUser(userId);
	}
	
	@ApiOperation(value = "编辑租户用户信息", notes = "编辑租户用户信息")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "tenantId", value = "用户id", required = true, dataType = "String", paramType = "query"),
		@ApiImplicitParam(name = "tenantUserInfoDTO", value = "用户信息DTO", required = true, dataType = "TenantUserInfoDTO", paramType = "body")
	})
	@PutMapping(value = "/update/info/{tenantId}", produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Response<Boolean> updateTenantUser(@PathVariable("tenantId") String tenantId,
	                                          @RequestBody TenantUserInfoDTO tenantUserInfoDTO) {
		Long userId = Long.valueOf(tenantId);
		return iTenantUserService.updateTenantUser(userId, tenantUserInfoDTO);
	}
	
	@ApiOperation(value = "根据用户名，密码登陆", notes = "根据用户名，密码登陆")
	@ApiImplicitParam(name = "loginDTO", value = "登陆DTO", required = true, dataType = "LoginDTO", paramType = "body")
	@PostMapping(value = "/login", produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Response<TenantUserInfoVO> userLogin(@RequestBody LoginDTO loginDTO) {
		return iTenantUserService.userLogin(loginDTO);
	}
	
	@ApiOperation(value = "编辑租户登陆信息", notes = "编辑租户登陆信息")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "tenantId", value = "用户id", required = true, dataType = "Long", paramType = "query"),
		@ApiImplicitParam(name = "loginDTO", value = "用户信息DTO", required = true, dataType = "TenantLoginDTO", paramType = "body")
	})
	@PutMapping(value = "/update/{tenantId}", produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Response<Boolean> updateTenantLogin(@PathVariable("tenantId") Long tenantId,
	                                          @RequestBody TenantLoginDTO loginDTO) {
		return iTenantUserService.updateTenantLogin(tenantId, loginDTO);
	}
	
}
